package cn.stylefeng.guns.modular.erclassify.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.erclassify.entity.ErClassify;
import cn.stylefeng.guns.modular.erclassify.mapper.ErClassifyMapper;
import cn.stylefeng.guns.modular.erclassify.model.param.ErClassifyParam;
import cn.stylefeng.guns.modular.erclassify.service.ErClassifyService;
import cn.stylefeng.guns.modular.erclassify.enums.ErClassifyExceptionEnum;
import cn.stylefeng.guns.modular.erlist.entity.ErList;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 分类 服务实现类
 *
 * @author w
 * @date 2021/06/10 09:05
 */
@Service
public class ErClassifyServiceImpl extends ServiceImpl<ErClassifyMapper, ErClassify> implements ErClassifyService {

    @Override
    public PageResult<ErClassify> page(ErClassifyParam erClassifyParam) {

        // 构造条件
        LambdaQueryWrapper<ErClassify> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotEmpty(erClassifyParam)){
            if(ObjectUtil.isNotNull(erClassifyParam.getName())){
                queryWrapper.eq(ErClassify::getName,erClassifyParam.getName());
            }
        }
        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<ErClassify> list(ErClassifyParam erClassifyParam) {

        // 构造条件
        LambdaQueryWrapper<ErClassify> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(ErClassifyParam erClassifyParam) {

        // 将dto转为实体
        ErClassify erClassify = new ErClassify();
        BeanUtil.copyProperties(erClassifyParam, erClassify);

        this.save(erClassify);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(ErClassifyParam erClassifyParam) {
        this.removeById(erClassifyParam.getId());
    }

    @Override
    public void edit(ErClassifyParam erClassifyParam) {

        // 根据id查询实体
        ErClassify erClassify = this.queryErClassify(erClassifyParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(erClassifyParam, erClassify);

        this.updateById(erClassify);
    }

    @Override
    public ErClassify detail(ErClassifyParam erClassifyParam) {
        return this.queryErClassify(erClassifyParam);
    }



    /**
     * 获取分类
     *
     * @author w
     * @since 2021/06/10 09:05
     */
    private ErClassify queryErClassify(ErClassifyParam erClassifyParam) {
        ErClassify erClassify = this.getById(erClassifyParam.getId());
        if (ObjectUtil.isEmpty(erClassify)) {
            throw new ServiceException(ErClassifyExceptionEnum.NOT_EXISTED);
        }
        return erClassify;
    }
}